package com.webshopping.controller.shopcar;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.webshopping.entry.Advertisement;
import com.webshopping.entry.Commodity;
import com.webshopping.entry.ShopCar;
import com.webshopping.entry.User;
import com.webshopping.service.goods.CommodityService;
import com.webshopping.service.goods.ShopCarService;

@Controller
@RequestMapping("shopcar/")
public class ShopCarController {

	@Autowired
	private ShopCarService scService;
	@Autowired
	private CommodityService cService;
	
	@RequestMapping(value = "queryShopcarInfo")
	public ModelAndView queryShopcarInfo(HttpServletRequest request,ShopCar sc){
		System.out.println(sc.getUserId());
		ModelAndView view = new ModelAndView();
		List<ShopCar> shopCarList = scService.query(sc);
		
		if (shopCarList != null) {
			for(ShopCar a:shopCarList){
	       
			if (a.getImg()!= null) {
				System.out.println(a.getImgName());
				String imgPath = request.getSession().getServletContext().getRealPath("Imgs");
				File file = new File(imgPath + "/" + a.getImgName());
				if (!file.exists()) {
					InputStream input = new ByteArrayInputStream(a.getImg());
					try {
						FileUtils.copyInputStreamToFile(input, file);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			 }
			}
		}
			System.out.println(shopCarList.size());		
		view.addObject("shopCarList", shopCarList);
		view.setViewName("../web/shopcart.jsp");
		//view.setViewName("../manager/adv/advertisementQuery.jsp");
		return view;
		
	}
	
	@RequestMapping(value = "shopcartAddNum")
	@ResponseBody
	public ModelMap addNum(int id,int commodityId){
		System.out.println(id);
		System.out.println(commodityId);
		ModelMap m = new ModelMap();
		Commodity cc = cService.get(commodityId);
		cc.setNumber(cc.getNumber()-1);
		cService.update(cc);
		scService.increase(id);
		m.addAttribute("success", true);
		return m;
	}
	
	@RequestMapping(value = "shopcartcutNum")
	@ResponseBody
	public ModelMap cutNum(int id,int commodityId){
		ModelMap m = new ModelMap();
		Commodity cc = cService.get(commodityId);
		cc.setNumber(cc.getNumber()+1);
		cService.update(cc);
		scService.decrease(id);
		m.addAttribute("success", true);
		return m;
	}
	
	@RequestMapping(value = "deleteShopCarInfo")
	public String deleteShopCar(int id){
		int rows = scService.delete(id);
		return "queryShopcarInfo.do";
	}
	
	@RequestMapping(value = "addShopcart")
	@ResponseBody
	public ModelMap addShopCar(ShopCar sc){
		//System.out.println("123");
		System.out.println(sc);
		ModelMap m = new ModelMap();
		System.out.println(sc);
		Commodity cc = cService.get(sc.getCommodityId());
		if(cc.getNumber()>=1){
			cc.setNumber(cc.getNumber()-1);
			cService.update(cc);
		}		
		List<ShopCar> res = scService.queryHave(sc);
		if(res.size()>0){			
			scService.increase(res.get(0).getId());
		}else{
			sc.setNum(1);
			scService.add(sc);
		}
		//System.out.println(res);
		return m;
	}
}
